package com.zdkj.admin.controller;/**
 * @author 陈博
 * @date 2020/5/13 0013 9:48
 */

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zdkj.admin.api.dto.NoticeDto;
import com.zdkj.admin.api.dto.NoticeOutDTO;
import com.zdkj.admin.api.entity.SwStNoticMessage;
import com.zdkj.admin.api.entity.SwStWebNotic;
import com.zdkj.admin.api.entity.SysUser;
import com.zdkj.admin.job.SysNoticeJob;
import com.zdkj.admin.service.SwStNoticMessageService;
import com.zdkj.admin.service.SwStWebNoticService;
import com.zdkj.admin.service.SysRoleService;
import com.zdkj.admin.service.SysUserService;
import com.zdkj.common.core.util.R;
import com.zdkj.common.data.tenant.TenantContextHolder;
import com.zdkj.common.log.annotation.SysLog;
import com.zdkj.common.security.util.ObjectUtils;
import com.zdkj.common.security.util.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @program: zdkj
 *
 * @description: 公共通知
 *
 * @author: chen
 *
 * @create: 2020-05-13 09:48
 **/
@RestController
@Slf4j
@Api(value = "notice", tags = "通知管理模块")
public class WebNoticeController {

    @Autowired
    private SimpMessagingTemplate simpMessagingTemplate;

    @Autowired
    private SwStWebNoticService swStWebNoticService;

    @Autowired
    private SwStNoticMessageService swStNoticMessageService;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysRoleService sysRoleService ;

    @Autowired
    private SysNoticeJob sysNoticeJob;

    private Page page=new Page();

    /**
     * 发送通知到单个用户
     * @param noticeDto
     * @return
     */
    @SysLog("发送通知到单个用户")
    @ApiOperation(value = "发送通知到单个用户", notes = "发送通知到单个用户")
    @PostMapping("/sendToOneUser")
    public R sendToOneUser(@RequestBody NoticeDto noticeDto){
        SwStWebNotic defaultObj = ObjectUtils.getDefaultObj(SwStWebNotic.class);
        BeanUtil.copyProperties(noticeDto, defaultObj, CopyOptions.create().setIgnoreNullValue(true));
        swStWebNoticService.save(defaultObj);
        SwStNoticMessage swStNoticMessage = new SwStNoticMessage();
        swStNoticMessage.setContent(noticeDto.getContent());
        swStNoticMessage.setNoticeId(defaultObj.getId());
        swStNoticMessageService.save(swStNoticMessage);
        /**
         * 发送通知的提示bean
         */
        NoticeOutDTO noticeOutDTO = new NoticeOutDTO();
        noticeOutDTO.setType(noticeDto.getType());
        noticeOutDTO.setBusinessTopic(noticeDto.getBusinessTopic());
        noticeOutDTO.setNoticeId(swStNoticMessage.getNoticeId());


        SysUser user = sysUserService.getOne(Wrappers.<SysUser>query().lambda().eq(SysUser::getUserCode, noticeDto.getReceiveUserCode()));
        /**
         * 发送异步通知
         */
        sysNoticeJob.sendUser(user,noticeOutDTO);
        return R.ok("用户通知下发成功");
    }

    /**
     * 发送通知到组
     * @param noticeDto
     * @return
     */
    @SysLog("发送通知")
    @ApiOperation(value = "发送通知", notes = "发送通知到组")
    @PostMapping("/sendToGroup")
    public R sendToGroup(@RequestBody NoticeDto noticeDto){
        /**
         * 插入数据到通知表 并通知
          */
        setDefault(noticeDto);
        return R.ok("通知插入成功");
    }
    /**
     * 数据插入通知表
     *
     * gourp-type  0 是发送给单个用户  情景 私信
     *            非0是发送给群组，例如 部门，角色，线路，站点  多个的话请用 ','分隔 此时groups必传
     * @param noticeDto
     * @return
     */
    private R setDefault(NoticeDto noticeDto) {
        if (noticeDto.getGroupType().equals("0")) {
            if (null == noticeDto.getGroups()) {
                return R.failed("通知方式为一对一，请传入receiveUserCode");
            }
            SwStWebNotic defaultObj = ObjectUtils.getDefaultObj(SwStWebNotic.class);
            BeanUtil.copyProperties(noticeDto, defaultObj, CopyOptions.create().setIgnoreNullValue(true));
            swStWebNoticService.save(defaultObj);
            SwStNoticMessage swStNoticMessage = new SwStNoticMessage();
            swStNoticMessage.setContent(noticeDto.getContent());
            swStNoticMessage.setNoticeId(defaultObj.getId());
            swStNoticMessageService.save(swStNoticMessage);
            /**
             * 发送通知的提示bean
             */
            NoticeOutDTO noticeOutDTO = new NoticeOutDTO();
            noticeOutDTO.setType(noticeDto.getType());
            noticeOutDTO.setBusinessTopic(noticeDto.getBusinessTopic());
            noticeOutDTO.setNoticeId(swStNoticMessage.getNoticeId());


            SysUser user = sysUserService.getOne(Wrappers.<SysUser>query().lambda().eq(SysUser::getUserCode, noticeDto.getReceiveUserCode()));
            /**
             * 发送异步通知
             */
            sysNoticeJob.sendUser(user, noticeOutDTO);
            return R.ok("通知插入成功");
        } else {
            if (null == noticeDto.getGroups()) {
                return R.failed("通知方式为群组，请传入groups");
            }
            List<String> strings = Arrays.asList(noticeDto.getGroups().split(","));
            IPage<SysUser> page = null;
            switch (noticeDto.getGroupType()) {
                case "1":
                    page = sysUserService.page(this.page.setSize(-1), Wrappers.<SysUser>query().lambda().in(SysUser::getDeptId, strings));
                    break;
                case "2":
                    page = sysUserService.page(this.page.setSize(-1), Wrappers.<SysUser>query().lambda().in(SysUser::getLineCode, strings));
                    break;
                case "3":
                    page = sysUserService.page(this.page.setSize(-1), Wrappers.<SysUser>query().lambda().in(SysUser::getStationCode, strings));
                    break;
                case "4":
                    page = sysRoleService.listByIdsLinkUser(this.page.setSize(-1), strings);
                    break;
            }

            page.getRecords().forEach(user -> {
                SwStWebNotic defaultObj = ObjectUtils.getDefaultObj(SwStWebNotic.class);
                BeanUtil.copyProperties(noticeDto, defaultObj, CopyOptions.create().setIgnoreNullValue(true));
                defaultObj.setReceiveUserCode(user.getUserCode());
                swStWebNoticService.save(defaultObj);
                SwStNoticMessage swStNoticMessage = new SwStNoticMessage();
                swStNoticMessage.setContent(noticeDto.getContent());
                swStNoticMessage.setNoticeId(defaultObj.getId());
                swStNoticMessageService.save(swStNoticMessage);
                /**
                 * 发送通知的提示bean
                 */
                NoticeOutDTO noticeOutDTO = new NoticeOutDTO();
                noticeOutDTO.setType(noticeDto.getType());
                noticeOutDTO.setBusinessTopic(noticeDto.getBusinessTopic());
                noticeOutDTO.setNoticeId(swStNoticMessage.getNoticeId());

                sysNoticeJob.sendUser(user, noticeOutDTO);
            });

            return R.ok("通知插入成功");
        }
}





}
